package com.xjh.utils.pageutils;

import java.util.List;

import com.github.pagehelper.ISelect;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.xjh.utils.base.BaseBO;
import com.xjh.utils.beanutils.BeanCopyUtils;
import com.xjh.utils.datautils.SqlUtil;
import com.xjh.utils.datautils.StringUtil;
import com.xjh.utils.pageutils.IPage;

public class PagePlugin {
    public PagePlugin() {
    }

    public static <T extends BaseBO> IPage<T> startPage(Integer pageNum, Integer pageSize, ISelect select) {
        if (null != pageNum && null != pageSize) {
            Page<T> BaseBOS = PageHelper.startPage(pageNum, pageSize).doSelectPage(select);
            return pageConversion(BaseBOS);
        } else {
            return new IPage();
        }
    }

    public static <T extends BaseBO> IPage<T> startPage(Integer pageNum, Integer pageSize, String orderBy, ISelect select) {
        if (null != pageNum && null != pageSize) {
            if (StringUtil.isNotEmpty(orderBy)) {
                Page<T> BaseBOS = PageHelper.startPage(pageNum, pageSize, SqlUtil.escapeOrderBySql(getOrderBy(orderBy))).doSelectPage(select);
                return pageConversion(BaseBOS);
            } else {
                return startPage(pageNum, pageSize, select);
            }
        } else {
            return new IPage();
        }
    }

    public static <S, T> IPage<T> copyPage(IPage<S> sourcePage, Class<T> cls) {
        IPage<T> targetPage = new IPage();
        if (null != sourcePage) {
            targetPage.setPageNum(sourcePage.getPageNum());
            targetPage.setPageSize(sourcePage.getPageSize());
            targetPage.setTotalPage(sourcePage.getTotalPage());
            targetPage.setTotalSize(sourcePage.getTotalSize());
            targetPage.setOffset(sourcePage.getOffset());
            List<S> sourceList = sourcePage.getResult();
            if (null != sourceList && !sourceList.isEmpty()) {
                List<T> listVO = BeanCopyUtils.copyList(sourceList, cls);
                targetPage.setResult(listVO);
            }

            return targetPage;
        } else {
            return null;
        }
    }

    private static String getOrderBy(String orderBy) {
        if (StringUtil.isNotEmpty(orderBy)) {
            orderBy = StringUtil.toUnderScoreCase(orderBy);
            orderBy = orderBy.replaceAll("\\+([\\w]+)", " $1 ASC,");
            orderBy = orderBy.replaceAll("\\-([\\w]+)", " $1 DESC,");
            orderBy = orderBy.substring(0, orderBy.length() - 1);
            return orderBy;
        } else {
            return orderBy;
        }
    }

    private static <T extends BaseBO> IPage<T> pageConversion(Page<T> BaseBOS) {
        List<T> result = BaseBOS.getResult();
        IPage<T> page = new IPage();
        page.setPageNum(BaseBOS.getPageNum());
        page.setPageSize(BaseBOS.getPageSize());
        page.setTotalPage(BaseBOS.getPages());
        page.setTotalSize(BaseBOS.getTotal());
        page.setResult(result);
        return page;
    }
}
